package com.zbc.handler;

import com.zbc.constant.MessageConstant;
import com.zbc.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result handleSqlException(SQLIntegrityConstraintViolationException e) {
        String msg = e.getMessage();
        if(msg.contains("Duplicate entry")) {
            String[] infos = msg.split(" ");
            return Result.error(infos[2]+ MessageConstant.ALREADY_EXISTS);
        }
        return Result.error(MessageConstant.UNKNOWN_ERROR);
    }
}
